using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ButtonPickLiquidController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IEventSystemHandler
{
	private Color32 orginalIconColor = new Color32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue);

	private Color32 iconColorClicked;

	private Image iconImage;

	private Image circleImage;

	private Image backgroundImage;

	private float clickedSize = 1f;

	private float notClickedSize = 0.84f;

	private UnityAction onClickAction;

	private float animationSpeed = 0.25f;

	[HideInInspector]
	public bool canClick;

	private Vector3 startingPosition;

	public LiquidType liquidType;

	private float minSizeCircle = 0.4f;

	private float maxSizeCircle = 1f;

	private Vector3 endPosition;

	private Vector3 startPosition;

	private float slideInTime = 0.5f;

	private float slideOutTime = 0.5f;

	[HideInInspector]
	public bool endedSlideInAnimation;

	private bool zoomInAnimation;

	private bool zoomOutAnimation;

	[HideInInspector]
	public static bool blockAllButtonPickLiquidController;

	private void Awake()
	{
		iconImage = base.transform.FindDeepChild("Icon").GetComponent<Image>();
		backgroundImage = base.transform.FindDeepChild("Background").GetComponent<Image>();
		circleImage = base.transform.FindDeepChild("Circle").GetComponent<Image>();
		Transform transform = base.transform;
		float x = notClickedSize;
		float y = notClickedSize;
		Vector3 localScale = base.transform.localScale;
		transform.localScale = new Vector3(x, y, localScale.z);
		startingPosition = base.transform.position;
	}

	public void OnClick()
	{
		if (canClick && !blockAllButtonPickLiquidController && ZoomIn())
		{
			AudioController.instance.Play("pick_drink");
			if (onClickAction != null)
			{
				onClickAction();
			}
		}
	}

	public void SetOnClickAction(UnityAction action)
	{
		onClickAction = action;
	}

	public void SetLiquidType(LiquidType type)
	{
		liquidType = type;
		ButtonColorsData colorDataForButton = Globals.instance.buttonsColorsDataManager.GetColorDataForButton(type);
		iconColorClicked = colorDataForButton.iconColorClicked;
		iconImage.sprite = Resources.Load<Sprite>(colorDataForButton.iconImagePath);
		backgroundImage.sprite = Resources.Load<Sprite>(colorDataForButton.backgroundImagePath);
	}

	public bool ZoomIn()
	{
		if (!zoomInAnimation)
		{
			zoomInAnimation = true;
			zoomOutAnimation = false;
			iTween.StopByName(base.gameObject, "zoom");
			GameObject gameObject = base.gameObject;
			object[] obj = new object[12]
			{
				"from",
				null,
				null,
				null,
				null,
				null,
				null,
				null,
				null,
				null,
				null,
				null
			};
			Vector3 localScale = base.transform.localScale;
			obj[1] = localScale.x;
			obj[2] = "to";
			obj[3] = clickedSize;
			obj[4] = "onupdate";
			obj[5] = "OnUpdateAnimation";
			obj[6] = "time";
			obj[7] = animationSpeed;
			obj[8] = "oncomplete";
			obj[9] = "OnZoomInEnd";
			obj[10] = "name";
			obj[11] = "zoom";
			iTween.ValueTo(gameObject, iTween.Hash(obj));
			return true;
		}
		return false;
	}

	public void ZoomOut()
	{
		if (!zoomOutAnimation && endedSlideInAnimation)
		{
			zoomInAnimation = false;
			zoomOutAnimation = true;
			iTween.StopByName(base.gameObject, "zoom");
			GameObject gameObject = base.gameObject;
			object[] obj = new object[12]
			{
				"from",
				null,
				null,
				null,
				null,
				null,
				null,
				null,
				null,
				null,
				null,
				null
			};
			Vector3 localScale = base.transform.localScale;
			obj[1] = localScale.x;
			obj[2] = "to";
			obj[3] = notClickedSize;
			obj[4] = "onupdate";
			obj[5] = "OnUpdateAnimation";
			obj[6] = "time";
			obj[7] = animationSpeed;
			obj[8] = "oncomplete";
			obj[9] = "OnZoomOutEnd";
			obj[10] = "name";
			obj[11] = "zoom";
			iTween.ValueTo(gameObject, iTween.Hash(obj));
		}
	}

	public void OnZoomInEnd()
	{
	}

	public void OnZoomOutEnd()
	{
	}

	public void OnUpdateAnimation(float value)
	{
		float num = clickedSize * value;
		Transform transform = base.transform;
		float x = num;
		float y = num;
		Vector3 localScale = base.transform.localScale;
		transform.localScale = new Vector3(x, y, localScale.z);
		float num2 = (value - notClickedSize) / (clickedSize - notClickedSize);
		Image image = circleImage;
		Color color = circleImage.color;
		float r = color.r;
		Color color2 = circleImage.color;
		float g = color2.g;
		Color color3 = circleImage.color;
		image.color = new Color(r, g, color3.b, num2);
		iconImage.color = Color.Lerp(orginalIconColor, iconColorClicked, num2);
		float num3 = minSizeCircle + (value - notClickedSize) * (maxSizeCircle - minSizeCircle) / (clickedSize - notClickedSize);
		Transform transform2 = circleImage.transform;
		float x2 = num3;
		float y2 = num3;
		Vector3 localScale2 = circleImage.transform.localScale;
		transform2.localScale = new Vector3(x2, y2, localScale2.z);
	}

	public void OnPointerDown(PointerEventData eventData)
	{
		BottleController.blockAllBottlesController = true;
	}

	public void OnPointerUp(PointerEventData eventData)
	{
		BottleController.blockAllBottlesController = false;
	}

	public void SetStartAndEndPosition(Vector3 start, Vector3 end)
	{
		startPosition = start;
		endPosition = end;
	}

	public void SlideInAnimation()
	{
		endedSlideInAnimation = false;
		canClick = true;
		iTween.StopByName(base.gameObject, "slide");
		iTween.MoveTo(base.gameObject, iTween.Hash("x", endPosition.x, "y", endPosition.y, "time", slideInTime, "islocal", true, "oncomplete", "OnEndSlideInAnimation", "oncompletetarget", base.gameObject, "name", "slide"));
	}

	public void OnEndSlideInAnimation()
	{
		endedSlideInAnimation = true;
	}

	public void SlideOutAnimation()
	{
		canClick = false;
		iTween.StopByName(base.gameObject, "slide");
		iTween.MoveTo(base.gameObject, iTween.Hash("x", startPosition.x, "y", startPosition.y, "time", slideOutTime, "islocal", true, "oncomplete", "OnEndSlideOutAnimation", "oncompletetarget", base.gameObject, "name", "slide"));
	}

	public void OnEndSlideOutAnimation()
	{
		UnityEngine.Object.Destroy(base.gameObject);
	}
}
